<?php
namespace app\oem\controller;
use think\Controller;
use think\Cache;
use think\Db;
use think\request;
use app\common\controller\OemBaseController;

class Helps extends OemBaseController
{
	//帮助列表
    public function helps()
    {
    	if(input("pid")){
    		$pid=input('pid');
    	}else{
    		$pid='1,2,3,4,5,6';
    	}
    	$list = Db::name('sys_helps')->where('pid in ('.$pid.')')->order('add_time desc')->paginate(8);
        $this->assign('list',$list);
        $this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    //帮助详情
    public function helps_show()
    {
    	$id=input('id');
    	$data = Db::name('sys_helps')->where('id',$id)->find();
    	$this->assign('data',$data);
    	return $this->fetch();
    }
    
    
    //更新日志
    public function iteration()
    {
    	$list = Db::name('sys_iteration')->order('add_time desc')->select();
        $this->assign('list',$list);
        $this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    
    
    //平台公告OEM发布
    public function notice()
    {
    	if(session('user_level')==1){
    		$id = session('d_id');
    	}else{
    		$id = session('oem_id');
    	}
    	$list = Db::name('sys_notice')->where('oem_id',$id)->order('add_time desc')->paginate(8);
        $this->assign('list',$list);
        $this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    //平台公告详情
    public function notice_show()
    {
    	$id=input('id');
    	$data = Db::name('sys_notice')->where('id',$id)->find();
    	$this->assign('data',$data);
    	return $this->fetch();
    }
    //平台公告增加强
    public function notice_add()
    {
    	if($_POST){
            $data['title']		=$_POST['title'];
            $data['content']	=$_POST['content'];
            $data['oem_id']		=session('d_id');
            $data['add_time'] 	=now_time();
            $result=Db::name('sys_notice')->insert($data);//分组数据库
            if($result){
			  return json(['code'=>200,'msg'=>'成功']);
		    }else{
		      return json(['code'=>402,'msg'=>'失败']);
		    }
            
        }
    	return $this->fetch();
    }
    //平台公告删除
    public function notice_del()
    {
    	$result =Db::name('sys_notice')->where('id', input("id"))->delete();;
        if($result){
		 return json(['code'=>200,'msg'=>'成功']);
	    }else{
	      return json(['code'=>402,'msg'=>'失败']);
	    } 
    }
    
    //联系我们
	public function contact(){
		if ($this->request->isPost()) {
            $site_config =  input("content");			
            $data['value'] = serialize($site_config);
            if (Db::name('tg_admin_system')->where('name', 'contact_config')->update($data) !== false) {
                Cache::set('gl_config', $site_config);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
        	if (Cache::has('contact_config')) {
	            $site_config = Cache::get('contact_config');
	        } else {
	            $site_config = Db::name('tg_admin_system')->field('value')->where('name', 'contact_config')->find();
	            $site_config = unserialize($site_config['value']);
	        }
        	return $this->fetch('contact', ['contact_config' => $site_config]);
        }
		return $this->fetch();
	}
	
	//服务协议
	public function service(){
		if ($this->request->isPost()) {
            $site_config =  input("content");
            $data['value'] = $site_config;
            if (Db::name('system')->where('name', 'service_config')->update($data) !== false) {
                Cache::set('service_config', $site_config);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
        	if (Cache::has('service_config')) {
	            $site_config = Cache::get('service_config');
	        } else {
	            $site_config = Db::name('system')->field('value')->where('name', 'service_config')->find();
	            $site_config = $site_config['value'];
	        }
        	return $this->fetch('service', ['service_config' => $site_config]);
        }
		return $this->fetch();
	}
    
	//帮助中心
	public function center()
	{
		if ($this->request->isPost()) {
            $site_config =  input("content");
            $data['value'] = $site_config;
            if (Db::name('system')->where('name', 'center_config')->update($data) !== false) {
                Cache::set('center_config', $site_config);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
        	if (Cache::has('center_config')) {
	            $site_config = Cache::get('center_config');
	        } else {
	            $site_config = Db::name('system')->field('value')->where('name', 'center_config')->find();
	            $site_config = $site_config['value'];
	        }
        	return $this->fetch('center', ['center_config' => $site_config]);
        }
		return $this->fetch();
	}
    
}
